﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Besture.Bll;
using Besture.Model;
using Besture.Common.Definition;
using Besture.Utils;
using System.Data;

namespace Besture.UI.Member
{
    public partial class Safety : System.Web.UI.Page
    {
        //Banner图
        public ImageModel _BannerTop { get; set; }
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                InitData();
            }
            GetBanner();
        }

        #region 初始化数据
        private void InitData()
        {
            UserModel user = Session[Constants.SESSION_KEY_LOGIN_USER] as UserModel;
            if (user != null)
                txtPhone.Value = user.Phone;
        }

        #endregion

        #region 保存

        protected void btnSubmit_Click(object sender, EventArgs eventArgs)
        {
            UserDac dac = new UserDac();
            UserModel model = Session[Constants.SESSION_KEY_LOGIN_USER] as UserModel;
            if (model == null)
            {
                Fun.Alert("请先登录","/Land/Land.aspx");
            }
            DataTable dt = dac.GetDataTable(model.Phone);
            //如果存在返回1
            if (dt != null && dt.Rows.Count > 0 && dt.Rows[0]["ID"].ToString() != model.ID.ToString())
            {
                Fun.Alert(this.Page,"手机号码已存在");
                return;
            }

            model = dac.GetModel(model.ID);
            string oldPw = txtPassword.Value.Trim();
            string newPw = txtPasswordNew.Value.Trim();
            string newComPw = txtPasswordNewCom.Value.Trim();
            string mobile = txtPhone.Value.Trim();
            if (string.IsNullOrEmpty(oldPw))
            {
                Fun.Alert(this.Page,"旧密码不能为空");
                return;
            }
            if (string.IsNullOrEmpty(newPw))
            {
                Fun.Alert(this.Page,"新密码不能为空");
                return;
            }
            if (newPw != newComPw)
            {
                Fun.Alert(this.Page,"新密码与确认密码不符");
                return;
            }
            oldPw = Fun.GetMD5(oldPw);
            if (model.Password != oldPw)
            {
                Fun.Alert(this.Page,"旧密码不正确");
                return;
            }
            if (string.IsNullOrEmpty(mobile))
            {
                Fun.Alert(this.Page,"手机号不能为空");
                return;
            }
            model.Password =Fun.GetMD5(newPw);
            model.Phone = mobile;
            model.IsDeleted = 0;
            model.UpdateTime = DateTime.Now;

            int re = dac.Update(model);
            if (re > 0)
            {
                Session[Constants.SESSION_KEY_LOGIN_USER] = model;
                Fun.Alert(this.Page,"保存成功");
            }
            else
            {
                Fun.Alert(this.Page,"保存失败");
            }
        }
        #endregion

        #region 获取Banner
        /// <summary>
        /// 获取Banner
        /// </summary>
        public void GetBanner()
        {
            ImageDac imageDac = new ImageDac();
            ImageModel imgModel = imageDac.GetDataTableByCode(Constants.DICTIONARY_CODE_BannerMember, Constants.DICTIONARY_TYPE_IMAGES);
            if (imgModel != null)
                _BannerTop = imgModel;
            else
                _BannerTop = new ImageModel();
        }
        #endregion
    }
}